package com.zh.authserver.web;

import com.zh.authserver.common.ApiResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.zh.authserver.common.ApiResult.success;

/**
 * @author ZH
 * @date 9:08 2020/8/9
 */
@RestController
public class LoginUserInfoController {

    @GetMapping("/hello")
    public String hello() {
        System.out.println("call hello ...");
        var authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            System.out.println("authentication name = "+authentication.getName()+" , isAuthenticated = " + authentication.isAuthenticated());
        }
        return "hello world";
    }

    /**
     * 当前用户的登录信息
     *
     * @return
     */
    //spring oauth2
    @GetMapping("/oauth2/auth")
    public ApiResult<Map> userAuthentication2() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("authentication = " + authentication);
        return success(Map.of(
                "Principal", authentication.getPrincipal(),
                "Credentials", authentication.getCredentials(),
                "Details", authentication.getDetails()
        ));
    }

}
